﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_ManageCourses : System.Web.UI.Page
{
    LMSData.CourseController ccontrol = new LMSData.CourseController();
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Roles.IsUserInRole(Membership.GetUser().UserName, "Admin") && !Roles.IsUserInRole(Membership.GetUser().UserName, "Instructor"))
                Response.Redirect("~/");
        }
        catch
        {
            Response.Redirect("~/");
        }

        if(!IsPostBack)
            BindCourses();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindCourses();
    }

    private void BindCourses()
    {
        LMSData.CourseCollection cc = new LMSData.CourseCollection();
        cc = ccontrol.GetCoursesByName(txtName.Text);
        if (cc.Count > 0)
        {
            pnlResults.Visible = true;
        }
        grdCourses.DataSource = cc;
        grdCourses.DataBind();
    }

    protected void SetVisibilityW(object sender, EventArgs e)
    {
        HyperLink hl = (HyperLink)sender;
        HiddenField hf = (HiddenField)hl.FindControl("hidID1");
        System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Server.MapPath("~/Course/Courses/"));
        bool Exists = false;
        foreach (System.IO.DirectoryInfo dinfo in di.GetDirectories())
        {
            if (dinfo.Name == hf.Value)
                Exists = true;
        }
        if (!Exists)
            hl.Visible = false;

        hl.NavigateUrl = "~/Course/Nav.aspx?NT=W&FN=" + hf.Value;
    }

    protected void SetVisibilitySL(object sender, EventArgs e)
    {
        HyperLink hl = (HyperLink)sender;
        HiddenField hf = (HiddenField)hl.FindControl("hidID2");
        System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Server.MapPath("~/Course/Courses/"));
        bool Exists = false;
        foreach (System.IO.DirectoryInfo dinfo in di.GetDirectories())
        {
            if (dinfo.Name == hf.Value)
                Exists = true;
        }
        if (!Exists)
            hl.Visible = false;

        hl.NavigateUrl = "~/Course/Nav.aspx?NT=SL&FN=" + hf.Value;
    }

    protected void ChangePage(object sender, GridViewPageEventArgs e)
    {
        grdCourses.PageIndex = e.NewPageIndex;
        BindCourses();
    }

    protected void DeleteCourse(object sender, CommandEventArgs e)
    {
        LMSData.Course crs = new LMSData.Course(e.CommandArgument.ToString());
        crs.IsDeleted = true;
        crs.Save();
        BindCourses();
    }

    /*
        <asp:TemplateField HeaderText="Launch SL">
        <ItemTemplate>
            <asp:HyperLink ID="lnkSLLaunch" runat="server" Text="Launch" OnPreRender="SetVisibilitySL"></asp:HyperLink>
            <asp:HiddenField ID="hidID2" runat="server" Value='<%# Eval("CourseID") %>' />
        </ItemTemplate>
    </asp:TemplateField>
    */

}
